<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("lib/validate.php");
//Includes
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class CompanyRegistration extends Page
{
       public $lblfname = null;
       public $Label16 = null;
       public $Label13 = null;
       public $edOther = null;
       public $Label11 = null;
       public $edCity = null;
       public $edStreet = null;
       public $lblCity = null;
       public $lblStreet = null;
       public $cmbCountry = null;
       public $Label14 = null;
       public $edBldg = null;
       public $lblBldg = null;
       public $Label12 = null;
       public $Label10 = null;
       public $Label6 = null;
       public $AJAX_EMAIL_NEW = null;
       public $lblcontarea = null;
       public $edcontarea = null;
       public $Label5 = null;
       public $lblarea = null;
       public $edArea = null;
       public $Label4 = null;
       public $Button1 = null;
       public $btnSubmit = null;
       public $edcontnum = null;
       public $lblcontnum = null;
       public $edfname = null;
       public $Label8 = null;
       public $lbllname = null;
       public $edlname = null;
       public $Label9 = null;
       public $JTSiteTheme1 = null;
       public $JSValidate = null;
       public $hfError = null;
       public $hiddenNewCount = null;
       public $hiddenExist = null;
       public $hiddenAuto = null;
       public $Label3 = null;
       public $imgCaptchaRefresh = null;
       public $editCaptcha = null;
       public $Label15 = null;
       public $img2 = null;
       public $cmbbtype = null;
       public $Label2 = null;
       public $lblcompphone = null;
       public $Label29 = null;
       public $edcompphone = null;
       public $Label24 = null;
       public $lblEmail = null;
       public $Label7 = null;
       public $edEmail = null;
       public $lblCompanyName = null;
       public $edCompanyName = null;
       public $Label1 = null;
       function cmbbtypeJSChange($sender, $params)
       {

       ?>
       //Add your javascript code here
       var btype = document.getElementById('cmbbtype').value;

       if(btype=='Other')
       {
       //alert(btype);
       document.getElementById("edOther").disabled=false;
       document.getElementById("edOther").style.backgroundColor='#000000';
       }
       else
       {
       //alert(btype);
       document.getElementById("edOther").disabled=true;
       document.getElementById("edOther").style.backgroundColor='#707070';
       }
       <?php

       }



       function edCityJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edCity','lblCity');
       <?php

       }

       function edStreetJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edStreet','lblStreet');
       <?php

       }

       function edAddressJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edBldg','lblBldg');
       <?php

       }

       function edcontareaJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       var edcontarea = document.getElementById('edcontarea').value;
       validateRequire('edcontarea','lblcontarea');

       if(edcontarea.length>0)
       {
       regex_pattern = "^[0-9]{2,4}$";
       var reg = new RegExp(regex_pattern);
       var match = document.getElementById('edcontarea').value.match(reg);
       if(match==null)
       {
        document.getElementById('lblcontarea').innerHTML = "Incorrect format - e.g. 02&nbsp;&darr;";
       }
       else
       {
        document.getElementById('lblcontarea').innerHTML = "&nbsp;";
       }
       }
       <?php

       }

       function edAreaJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edArea','lblarea');

       var edArea = document.getElementById('edArea').value;

       if(edArea.length>0)
       {
       regex_pattern = "^[0-9]{2,4}$";
       var reg = new RegExp(regex_pattern);
       var match = document.getElementById('edArea').value.match(reg);
       if(match==null)
       {
        document.getElementById('lblarea').innerHTML = "Incorrect format - e.g. 02&nbsp;&darr;";
       }
       else
       {
       document.getElementById('lblarea').innerHTML = "";
       }
       }
       <?php

       }


       function btnSubmitJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('btnSubmit').src="image/btn_signup.png"
       <?php

       }

       function btnSubmitJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('btnSubmit').src="image/btn_signup2.png"
       <?php

       }



       function CompanyRegistrationJSLoad($sender, $params)
       {

       ?>
       document.images.img2.src = 'php_captcha.php?'+Math.random();

       var btype = document.getElementById('cmbbtype').value;

       if(btype=='Other')
       {
       //alert(btype);
       document.getElementById("edOther").disabled=false;
       document.getElementById("edOther").style.backgroundColor='#000000';
       }
       else
       {
       //alert(btype);
       document.getElementById("edOther").disabled=true;
       document.getElementById("edOther").style.backgroundColor='#707070';
       }

       <?php

       }

       function edcontnumJSBlur($sender, $params)
       {

       ?>
       validateRequire('edcontnum','lblcontnum');

       var edcontnum = document.getElementById('edcontnum').value;

       if(edcontnum.length>0)
       {
       regex_pattern = "^[0-9]{3}[-]?[0-9]{4}$";
       var reg = new RegExp(regex_pattern);
       var match = document.getElementById('edcontnum').value.match(reg);
       if(match==null)
       {
        document.getElementById('lblcontnum').innerHTML = "&darr;&nbsp;Incorrect format - e.g.899-1234&nbsp;&darr;";
       }
       else
       {
        document.getElementById('lblcontnum').innerHTML = "";
       }
       }
       <?php

       }

       function edfnameJSBlur($sender, $params)
       {

       ?>
       validateRequire('edfname','lblfname');
       <?php

       }

       function edlnameJSBlur($sender, $params)
       {

       ?>
       validateRequire('edlname','lbllname');
       <?php

       }


       function imgCaptchaRefreshJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images.img2.src = 'php_captcha.php?'+Math.random();
       <?php

       }

       function edEmailJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edEmail','lblEmail');

       var edEmail = document.getElementById('edEmail').value;

       if(edEmail.length>0)
       {
       regex_pattern = "[A-Za-z0-9_\.]+[@][A-Za-z-]+[.][A-Za-z]{2,4}([\.A-Za-z]{2,3})?$";
       var regm = new RegExp(regex_pattern);
       var match = document.getElementById('edEmail').value.match(regm);
       if(match==null)
       {
        document.getElementById('lblEmail').innerHTML = "&darr;&nbsp;Incorrect format - e.g.(user@mail.com)&nbsp;&darr;";
       }
       }
       <?php

       }

       function edcompphoneJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edcompphone','lblcompphone');

       var edcompphone = document.getElementById('edcompphone').value;

       if(edcompphone.length>0)
       {
       regex_pattern = "^[0-9]{3}[-]?[0-9]{4}$";
       var reg = new RegExp(regex_pattern);
       var match = document.getElementById('edcompphone').value.match(reg);
       if(match==null)
       {
        document.getElementById('lblcompphone').innerHTML = "&darr;&nbsp;Incorrect format - e.g.899-1234&nbsp;&darr;";
       }
       }
       <?php

       }

       function edBldgJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edBldg','lblBldg');
       <?php

       }

       function edCompanyNameJSBlur($sender, $params)
       {

       ?>
       validateRequire('edCompanyName','lblCompanyName');
       <?php

       }

       function btnSubmitClick($sender, $params)
       {
       $key=substr($_SESSION['key'],0,5);
       $number = $this->editCaptcha->Text;
       if($number!=$key)
       {
        //die('xxx');
        echo "<script type=text/javascript>
        alert('Please enter the correct string shown in the image.');
        </script>";
       }
       $this->validateRequired($this->edCompanyName->Text, 'lblCompanyName');
       $this->validateRequired($this->edCity->Text, 'lblCity');
       $this->validateRequired($this->edStreet->Text, 'lblStreet');
       $this->validateRequired($this->edlname->Text, 'lbllname');
       $this->validateRequired($this->edfname->Text, 'lblfname');

       $phone = $this->edcompphone->Text;
       $this->validateRequired($this->edcompphone->Text, 'lblcompphone');

       if($phone!='')
       {
       if(preg_match("/^[0-9]{3}[-]?[0-9]{4}$/", $phone))
       {
       $this->lblcompphone->Caption = '';
       }
       else
       {
       $this->lblcompphone->Caption = '&darr;Incorrect format - e.g.(02)899-1234&darr;';
       $this->hfError->setValue('1');
       }
       }


       $contact = $this->edcontnum->Text;
       $this->validateRequired($this->edcontnum->Text, 'lblcontnum');

       if($contact!='')
       {
       if(preg_match("/^[0-9]{3}[-]?[0-9]{4}$/", $contact))
       {
       $this->lblcontnum->Caption = '';
       }
       else
       {
       $this->lblcontnum->Caption = '&darr;&nbsp;Incorrect format - e.g.899-1234&nbsp;&darr;';
       $this->hfError->setValue('1');
       }
       }


       $acode = $this->edArea->Text;
       if($acode=='')
       $this->validateRequired($this->edArea->Text, 'lblarea');

       if($acode!='')
       {
       if(preg_match("/^[0-9]{2,4}$/", $acode))
       {
       $this->lblarea->Caption = '';
       }
       else
       {
       $this->lblarea->Caption = 'Incorrect format - e.g.02&nbsp;&darr;';
       $this->hfError->setValue('1');
       }
       }

       $acode2 = $this->edcontarea->Text;
       if($acode2=='')
       $this->validateRequired($this->edcontarea->Text, 'lblcontarea');

       if($acode2!='')
       {
       if(preg_match("/^[0-9]{2,4}$/", $acode2))
       {
       $this->lblcontarea->Caption = '';
       }
       else
       {
       $this->lblcontarea->Caption = 'Incorrect format - e.g.02&nbsp;&darr;';
       $this->hfError->setValue('1');
       }
       }


       $email = $this->edEmail->Text;
       if($email=='')
       $this->validateRequired($this->edEmail->Text, 'lblEmail');

       if($email!='')
       {
       if(preg_match("/[A-Za-z0-9_\.]+[@][A-Za-z-]+[.][A-Za-z]{2,4}([\.A-Za-z]{2,3})?$/", $email))
       {
       $this->lblEmail->Caption = '';
       }
       else
       {
       $this->lblEmail->Caption = '&darr;&nbsp;Incorrect format - e.g.(user@mail.com)&nbsp;&darr;';
       $this->hfError->setValue('1');
       }
       }

       $error = $this->hfError->getValue();
       if($error == '1' || $number != $key)
       {
          $this->hfError->setValue('0');
          return false;
       }
       else if($error == '0')
       {
       $result_company_count2 = mysql_query("SELECT * FROM lookup_company_count")
       or die("comp count2 ".mysql_error());
       $row_comp_count2 = mysql_fetch_array($result_company_count2);
       $number1 = $row_comp_count2['total_comp_count']+1;
       $number2 = 'AMPS00000'.$number1;



       mysql_query("INSERT INTO company_tbl (company_name,
       app_num,
       general_email,
       bldg,
       street,
       city,
       country,
       acode,
       comp_phone,
       business_type,
       other,
       lname,
       fname,
       contact_acode,
       contact_person_num)
       VALUES
       ('".$this->edCompanyName->Text."',
       '".$number2."',
       '".$this->edEmail->Text."',
       '".$this->edBldg->Text."',
       '".$this->edStreet->Text."',
       '".$this->edCity->Text."',
       '".$this->cmbCountry->getItemIndex()."',
       '".$this->edArea->Text."',
       '".$this->edcompphone->Text."',
       '".$this->cmbbtype->getItemIndex()."',
       '".$this->edOther->Text."',
       '".$this->edlname->Text."',
        '".$this->edfname->Text."',
       '".$this->edcontarea->Text."',
       '".$this->edcontnum->Text."')")or die("INSERT COMPANY - ".mysql_error());


       $result_company_id = mysql_query("SELECT LAST_INSERT_ID(company_id) AS comp_id FROM company_tbl ORDER BY company_id DESC LIMIT 0,1") or die("SELECT COMPANY ID - ".mysql_error());
       $row_company_id = mysql_fetch_array($result_company_id);
       $company_id = $row_company_id['comp_id'];

       mysql_query("INSERT INTO company_contact(company_id,
       fname,
       lname,
       acode,
       phone)

       VALUES
       ('".$company_id."',
       '".$this->edfname->Text."',
       '".$this->edlname->Text."',
       '".$this->edcontarea->Text."',
       '".$this->edcontnum->Text."')") or die("INSERT CONTACT - ".mysql_error());

       mysql_query("INSERT INTO accounts_tbl (username, company_id, company_name, status, admin, created_by)
       VALUES
       ('".$this->edEmail->Text."',
       '".$company_id."',
       '".$this->edCompanyName->Text."',
       '0',
       '1',
       'admin_amps_admin'
       )") or die("INSERT ACCOUNT - ".mysql_error());

       //$query_up_cont = "UPDATE company_contact SET company_id='".$company_id."' WHERE company_id='".$_SESSION['rand']."'";
        //mysql_query($query_up_cont) or die("UPDATE CONTACTS ".mysql_error());

       //start auto-generate vin (note:INSERT STATEMENT)
       //die($this->hiddenExist->getValue());
       if($this->hiddenExist->getValue()=='0')
       {
       mysql_query("INSERT INTO lookup_company_count (total_comp_count)
       VALUES ('1')")
       or die("INSERT COUNT ".mysql_error());
       }
       if($this->hiddenExist->getValue()=='1')
       {
       mysql_query("UPDATE lookup_company_count SET total_comp_count='".$number1."'")
       or die("UPDATE COUNT ".mysql_error());;
       }
       //end auto-generate vin

      mysql_query("INSERT INTO accounts_access_level
      (company_id,
      account_level,
      vehicle,
      part,
      tire,
      route,
      settings)

      VALUES
      ('".$company_id."',
      'Super User',
      '2',
      '2',
      '2',
      '2',
      '2')")or die("INSERT ACCESS LEVEL".mysql_error());

      mysql_query("INSERT INTO accounts_access_level
      (company_id,
      account_level,
      vehicle,
      part,
      tire,
      route,
      settings)

      VALUES
      ('".$company_id."',
      'User',
      '1',
      '1',
      '1',
      '1',
      '1')")or die("INSERT ACCESS LEVEL".mysql_error());



      echo "<script type=text/javascript>
      var xmlhttp
      if (window.XMLHttpRequest)
      {
      xmlhttp = XMLHttpRequest();
      }
      if (window.ActiveXObject)
      {
      xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
      }

      if (xmlhttp==null)
      {
      alert ('Your browser does not support AJAX!');
      }

      var comp = '".$this->edCompanyName->Text."';
      var email = '".$this->edEmail->Text."';
      var con_person = '".$this->edlname->Text.",".$this->edfname->Text."';
      var con_area = '".$this->edcontarea->Text."';
      var con_number = '".$this->edcontnum->Text."';

      var url='email_new_account.php';
      url=url+'?comp='+comp;
      url=url+'&email='+email;
      url=url+'&con_person='+con_person;
      url=url+'&con_area='+con_area;
      url=url+'&con_number='+con_number;

      xmlhttp.open('GET',url,true);
      xmlhttp.send(null);

       parent.document.getElementById('divreg').Show();
       document.images.img2.src = 'php_captcha.php?'+Math.random();
       //alert('Registration Successful! Kindly wait for the login information that will be forwarded to your email. Thank you!');
       //window.location.reload();
       //top.location='login.php';
       //window.close();
       </script>";
       }
       $this->clrFields();
       }


       function validateRequired($value,$lbl)
       {
        if($value=='' || $value==null)
        {
         $this->$lbl->Caption = '&darr; This field is required &darr;';
         $this->hfError->setValue('1');
        }
       }

       function clrFields()
       {
       $this->edCompanyName->Text='';
       $this->edOther->Text='';
       $this->edBldg->Text='';
       $this->edStreet->Text='';
       $this->edCity->Text='';
       $this->edArea->Text = '';
       $this->edcontarea->Text = '';
       $this->edcompphone->Text='';
       $this->edEmail->Text='';
       $this->edcontnum->Text='';
       $this->edfname->Text='';
       $this->edlname->Text='';
       $this->editCaptcha->Text='';
       $this->cmbbtype->setItemIndex('--Select--');
       }


       function CompanyRegistrationCreate($sender, $params)
       {
       $this->hfError->setValue(0);
       $this->lblCompanyName->Caption = '';
       $this->lblBldg->Caption = '';
       $this->lblStreet->Caption = '';
       $this->lblCity->Caption = '';
       $this->lblcompphone->Caption = '';
       $this->lblEmail->Caption = '';
       $this->lblfname->Caption = '';
       $this->lbllname->Caption = '';
       $this->lblcontnum->Caption = '';
       $this->lblarea->Caption = '';
       $this->lblcontarea->Caption = '';
       $this->clrFields();


       $this->cmbCountry->Clear();
       $this->cmbCountry->ItemIndex = "Philippines";
        $result_country = mysql_query("select * from countries order by country ASC");

        while($row_country = mysql_fetch_array($result_country))
        {
            $this->cmbCountry->AddItem($row_country['country'], null, $row_country['country']);
        }


       if(!isset($_SESSION['rand']))
       {
       $_SESSION['rand'] = rand() . "\n";
       }

       //start auto-generate vin
       //hiddenExist
       //hiddenNewCount
       $concat_string = 'AMPS';
       $result_company_count = mysql_query("SELECT * FROM lookup_company_count")
       or die("comp count ".mysql_error());

       if(mysql_num_rows($result_company_count)==0)
       {
        $this->hiddenAuto->setValue($concat_string.'000001');
        $this->hiddenExist->setValue('0');
       }
       if(mysql_num_rows($result_company_count)>0)
       {
        $row_comp_count = mysql_fetch_array($result_company_count);
        $number = $row_comp_count['total_comp_count']+1;

        $this->hiddenNewCount->setValue($number);

        $length = strlen($number);
        $num_zero = 6 - $length;
        $zero = '';
        while($num_zero>0)
         {
           $zero = $zero.'0';
           $num_zero--;
         }
        $this->hiddenAuto->setValue($concat_string.$zero.$number);
        //$this->edApnum->Text = $concat_string.$zero.$number;
        $this->hiddenExist->setValue('1');
       }
       //end auto-generate vin



       }

}

global $application;

global $CompanyRegistration;

//Creates the form
$CompanyRegistration=new CompanyRegistration($application);

//Read from resource file
$CompanyRegistration->loadResource(__FILE__);

//Shows the form
$CompanyRegistration->show();

?>